package com.ssis.graphics;

import com.ssis.core.Game;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.newdawn.slick.util.Log;
import org.newdawn.slick.util.ResourceLoader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Dialog {
	private static Text text;
	private static String[] output;
	private static NodeList dialogNodeList;
	private static NodeList lines;
	private static int lineNumber;
	private static final int LINE_WIDTH = 60;
	private static final int FACES_ROWS = 2;
	private static final int FACES_COLS = 2;
	private static Sprite faces;
	private static Sprite background;

	public static void setConversation(int index) {
		Node conversationNode = dialogNodeList.item(index);
		lines = ((Element)conversationNode).getElementsByTagName("line");
		lineNumber = 0;
		updateOutput();
	}
	
	/***
	 * increment to the next line in the conversation
	 * @return true iff we aren't out of lines
	 */
	public static boolean next() {
		if(lineNumber + 1 >= lines.getLength()) {
			return false;
		}
		lineNumber++;
		updateOutput();
		return true;
	}
	
	private static void updateOutput() {
		output = getProcessedString(getLine(lineNumber));
		faces.changeFrame(getFaceIndex(getCharacter(lineNumber)));
	}
	
	private static String getLine(int index) {
		return ((Node)((Element)((Element)lines.item(index)).getElementsByTagName("script").item(0)).getChildNodes().item(0)).getNodeValue().trim();
	}
	
	private static String getCharacter(int index) {
		return ((Node)((Element)((Element)lines.item(index)).getElementsByTagName("speaker").item(0)).getChildNodes().item(0)).getNodeValue().trim();
	}
	
	public static void render() {
		background.render();
		faces.render();
		text.print(150, 500, output[0]);
		text.print(150, 520, output[1]);
		text.print(150, 540, output[2]);
		text.print(150, 560, output[3]);
		text.print(150, 580, output[4]);
	}

	private static String[] getProcessedString(String input) {
		String[] output = new String[5];
		output[0] = "";
		output[1] = "";
		output[2] = "";
		output[3] = "";
		output[4] = "";
		
		if (input.length() < LINE_WIDTH) {
			output[0] = input;
			return output;
		}
		int breakone = input.substring(0, Math.min(LINE_WIDTH, input.length())).lastIndexOf(' ');
		Log.debug("1: " + breakone);
		output[0] = input.substring(0, breakone);
		
		if (input.length() - breakone < LINE_WIDTH) {
			output[1] = input.substring(breakone + 1);
			return output;
		}
		int breaktwo = breakone + input.substring(breakone, Math.min(breakone + LINE_WIDTH, input.length())).lastIndexOf(' ');
		Log.debug("2: " + breaktwo);
		output[1] = input.substring(breakone + 1, breaktwo);
		
		if (input.length() - breaktwo < LINE_WIDTH) {
			output[2] = input.substring(breaktwo + 1);
			return output;
		}
		int breakthree = breaktwo + input.substring(breaktwo, Math.min(breaktwo + LINE_WIDTH, input.length())).lastIndexOf(' ');
		Log.debug("3: " + breakthree);
		output[2] = input.substring(breaktwo + 1, breakthree);
		
		if (input.length() - breakthree < LINE_WIDTH) {
			output[2] = input.substring(breakthree + 1);
			return output;
		}
		int breakfour = breakthree + input.substring(breakthree, Math.min(breakthree + LINE_WIDTH, input.length())).lastIndexOf(' ');
		Log.debug("4: " + breakfour);
		output[3] = input.substring(breakthree + 1, breakfour);
		
		output[4] = input.substring(breakfour + 1);
		return output;
	}

	public static void init(Game game) {
		text = game.text();
		try {
			ResourceLoader.getResourceAsStream("res/dialog.xml");
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(ResourceLoader.getResourceAsStream("res/dialog.xml"));
			doc.getDocumentElement().normalize();
			dialogNodeList = doc.getElementsByTagName("conversation");
		} catch (Exception e) {
			Log.debug("Problem with dialog file!");
			e.printStackTrace();
		}
		faces = new Sprite("faces", 0, Game.HEIGHT-128, 0, FACES_ROWS, FACES_COLS);
		background = new Sprite("dialog_background", 0, Game.HEIGHT-128, 0, 4, 1);
	}
	
	private static int getFaceIndex(String name) {
		if(name.equals("rogue"))
			return 0;
		if(name.equals("cicerone"))
			return 1;
		if(name.equals("cutter"))
			return 2;
		return 3;
	}
}
